﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//using vroom.wcf.client.ServiceReference;
using vroom.db.model;

using System.Security.Principal;
using System.Diagnostics;
using log4net;
using System.Reflection;

namespace Service.Client
{
	class Client
	{
		private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.Name);

		//ServiceClient _client = new ServiceClient();


		static void Main(string[] args)
		{
			Client p = new Client();
			p.Start();
		}


		private void Start()
		{
			try
			{

				#region Run 3 Only
				
				Task t1 = Task.Factory.StartNew(() => { GetPositions(new DateTime(2014, 11, 21), 0); }, new System.Threading.CancellationToken(), TaskCreationOptions.None, TaskScheduler.Default);
				t1.Wait();


				Task t3 = Task.Factory.StartNew(() => { GetBucketedPositions(new DateTime(2015, 2, 3), 0); }, new System.Threading.CancellationToken(), TaskCreationOptions.None, TaskScheduler.Default);
				t3.Wait();

				#endregion


				//RunAllLoad();

			}
			catch (Exception e)
			{
				_log.Error(e.Message + Environment.NewLine + e.StackTrace);
			}
			finally
			{
				//_client.Close();
			}

		}


		private void RunAllLoad()
		{
			#region Position - No filtering on Portfolio - 3 Day

			Task[] taskArray = new Task[45];

			for (int i = 0; i < 5; i++)
				taskArray[i] = Task.Factory.StartNew(() => { GetPositions(new DateTime(2014, 11, 21), i); }, new System.Threading.CancellationToken(), TaskCreationOptions.None, TaskScheduler.Default);

			for (int i = 5; i < 10; i++)
				taskArray[i] = Task.Factory.StartNew(() => { GetPositions(new DateTime(2015, 02, 03), i); }, new System.Threading.CancellationToken(), TaskCreationOptions.None, TaskScheduler.Default);

			for (int i = 10; i < 15; i++)
				taskArray[i] = Task.Factory.StartNew(() => { GetPositions(new DateTime(2015, 01, 22), i); }, new System.Threading.CancellationToken(), TaskCreationOptions.None, TaskScheduler.Default);

			#endregion





			#region Risk Buckets - 3 Days

			for (int i = 30; i < 35; i++)
				taskArray[i] = Task.Factory.StartNew(() => { GetBucketedPositions(new DateTime(2014, 11, 21), i); }, new System.Threading.CancellationToken(), TaskCreationOptions.None, TaskScheduler.Default);

			for (int i = 35; i < 40; i++)
				taskArray[i] = Task.Factory.StartNew(() => { GetBucketedPositions(new DateTime(2015, 2, 2), i); }, new System.Threading.CancellationToken(), TaskCreationOptions.None, TaskScheduler.Default);

			for (int i = 40; i < 45; i++)
				taskArray[i] = Task.Factory.StartNew(() => { GetBucketedPositions(new DateTime(2015, 1, 29), i); }, new System.Threading.CancellationToken(), TaskCreationOptions.None, TaskScheduler.Default);

			#endregion

			//Wait for all tasks
			Task.WaitAll(taskArray);

		}



		private void GetPositions(DateTime dateTime, int rep)
		{
            //Stopwatch watch = new Stopwatch();

            //watch.Reset();

            //watch.Start();
            //Position[] res = _client.GetPositions(@"EMEA\F077772", dateTime);
            //watch.Stop();

            //if (rep == 1)
            //    _log.Warn("GetPositions --> Took in milliseconds: " + (watch.ElapsedMilliseconds) + ", Count:" + res.Count());
            //else
            //    _log.Debug("GetPositions --> Took in milliseconds: " + (watch.ElapsedMilliseconds) + ", Count:" + res.Count());

		}


		private void GetBucketedPositions(DateTime dateTime, int rep)
		{
            //Stopwatch watch = new Stopwatch();

            //watch.Reset();

            //watch.Start();
            //Bucket[] res = _client.GetBucketedPositions(dateTime);
            //watch.Stop();

            //if (rep == 1)
            //    _log.Warn("GetBucketedPositions --> Took in milliseconds: " + (watch.ElapsedMilliseconds) + ", Count:" + res.Count());
            //else
            //    _log.Debug("GetBucketedPositions --> Took in milliseconds: " + (watch.ElapsedMilliseconds) + ", Count:" + res.Count());

		}



	}
}
